package uiautomator;

import android.graphics.Point;
import android.graphics.Rect;
import android.os.Environment;
import android.os.SystemClock;
import android.util.Log;
import android.util.SparseArray;
import android.util.Xml;
import android.view.Display;
import android.view.accessibility.AccessibilityNodeInfo;
import android.view.accessibility.AccessibilityWindowInfo;
import android.widget.GridLayout;
import android.widget.GridView;
import android.widget.ListView;
import android.widget.TableLayout;

import androidx.test.internal.runner.listener.InstrumentationResultPrinter;

import com.fasterxml.jackson.annotation.JsonProperty;
import com.googlecode.jsonrpc4j.JsonRpcBasicServer;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.StringWriter;
import java.util.List;

import mirror.RefMethod;
import mirror.android.hardware.display.DisplayManagerGlobal;

import org.xmlpull.v1.XmlSerializer;

/* loaded from: classes2.dex */
public class AccessibilityNodeInfoDumper {
    private static final String LOGTAG = AccessibilityNodeInfoDumper.class.getSimpleName();
    private static final String[] NAF_EXCLUDED_CLASSES = {GridView.class.getName(), GridLayout.class.getName(), ListView.class.getName(), TableLayout.class.getName()};
    public static Throwable e;

    public static void dumpWindowToFile(AccessibilityNodeInfo root, int rotation, int width, int height) {
        File baseDir = new File(Environment.getDataDirectory(), "local");
        if (!baseDir.exists()) {
            baseDir.mkdir();
            baseDir.setExecutable(true, false);
            baseDir.setWritable(true, false);
            baseDir.setReadable(true, false);
        }
        dumpWindowToFile(root, new File(new File(Environment.getDataDirectory(), "local"), "window_dump.xml"), rotation, width, height);
    }

    public static void dumpWindowToFile(AccessibilityNodeInfo root, File dumpFile, int rotation, int width, int height) {
        if (root == null) {
            return;
        }
        long startTime = SystemClock.uptimeMillis();
        try {
            FileWriter writer = new FileWriter(dumpFile);
            XmlSerializer serializer = Xml.newSerializer();
            StringWriter stringWriter = new StringWriter();
            serializer.setOutput(stringWriter);
            serializer.startDocument("UTF-8", true);
            serializer.startTag(JsonProperty.USE_DEFAULT_NAME, "hierarchy");
            serializer.attribute(JsonProperty.USE_DEFAULT_NAME, "rotation", Integer.toString(rotation));
            dumpNodeRec(root, serializer, 0, width, height);
            serializer.endTag(JsonProperty.USE_DEFAULT_NAME, "hierarchy");
            serializer.endDocument();
            writer.write(stringWriter.toString());
            writer.close();
        } catch (IOException e) {
            Log.e(LOGTAG, "failed to dump window to file", e);
        }
        long endTime = SystemClock.uptimeMillis();
        Log.w(LOGTAG, "Fetch time: " + (endTime - startTime) + "ms");
    }

    public static void dumpWindowsToFile(SparseArray<List<AccessibilityWindowInfo>> allWindows, File dumpFile) {
        Object displayManager;
        int d;
        int nd;
        StringWriter stringWriter;
        SparseArray<List<AccessibilityWindowInfo>> sparseArray = allWindows;
        if (allWindows.size() == 0) {
            return;
        }
        Object displayManager2 = DisplayManagerGlobal.getInstance.call(new Object[0]);
        long startTime = SystemClock.uptimeMillis();
        try {
            FileWriter writer = new FileWriter(dumpFile);
            XmlSerializer serializer = Xml.newSerializer();
            StringWriter stringWriter2 = new StringWriter();
            serializer.setOutput(stringWriter2);
            int i = 1;
            serializer.startDocument("UTF-8", true);
            serializer.startTag(JsonProperty.USE_DEFAULT_NAME, "displays");
            int nd2 = allWindows.size();
            int d2 = 0;
            while (d2 < nd2) {
                int displayId = sparseArray.keyAt(d2);
                RefMethod<Display> refMethod = DisplayManagerGlobal.getRealDisplay;
                Object[] objArr = new Object[i];
                objArr[0] = Integer.valueOf(displayId);
                Display display = refMethod.call(displayManager2, objArr);
                if (display == null) {
                    displayManager = displayManager2;
                    d = d2;
                    nd = nd2;
                    stringWriter = stringWriter2;
                } else {
                    List<AccessibilityWindowInfo> windows = sparseArray.valueAt(d2);
                    if (windows.isEmpty()) {
                        displayManager = displayManager2;
                        d = d2;
                        nd = nd2;
                        stringWriter = stringWriter2;
                    } else {
                        serializer.startTag(JsonProperty.USE_DEFAULT_NAME, "display");
                        serializer.attribute(JsonProperty.USE_DEFAULT_NAME, JsonRpcBasicServer.ID, Integer.toString(displayId));
                        int rotation = display.getRotation();
                        Point size = new Point();
                        display.getRealSize(size);
                        int i2 = 0;
                        int d3 = windows.size();
                        while (true) {
                            displayManager = displayManager2;
                            int n = d3;
                            if (i2 >= n) {
                                break;
                            }
                            try {
                                int nd3 = nd2;
                                int nd4 = i2;
                                dumpWindowRec(windows.get(i2), serializer, nd4, size.x, size.y, rotation);
                                i2++;
                                stringWriter2 = stringWriter2;
                                displayManager2 = displayManager;
                                d2 = d2;
                                d3 = n;
                                display = display;
                                windows = windows;
                                nd2 = nd3;
                            } catch (IOException e) {
                                e = e;
                                Log.e(LOGTAG, "failed to dump window to file", e);
                                long endTime = SystemClock.uptimeMillis();
                                Log.w(LOGTAG, "Fetch time: " + (endTime - startTime) + "ms");
                            }
                        }
                        d = d2;
                        nd = nd2;
                        stringWriter = stringWriter2;
                        serializer.endTag(JsonProperty.USE_DEFAULT_NAME, "display");
                    }
                }
                d2 = d + 1;
                sparseArray = allWindows;
                stringWriter2 = stringWriter;
                displayManager2 = displayManager;
                nd2 = nd;
                i = 1;
            }
            serializer.endTag(JsonProperty.USE_DEFAULT_NAME, "displays");
            serializer.endDocument();
            writer.write(stringWriter2.toString());
            writer.close();
        } catch (IOException e2) {
            e = e2;
            Log.e(LOGTAG, "failed to dump window to file", e);
            long endTime2 = SystemClock.uptimeMillis();
            Log.w(LOGTAG, "Fetch time: " + (endTime2 - startTime) + "ms");
        }
        long endTime22 = SystemClock.uptimeMillis();
        Log.w(LOGTAG, "Fetch time: " + (endTime22 - startTime) + "ms");
    }

    public static String dumpWindow(AccessibilityNodeInfo root, int rotation, int width, int height) {
        if (root == null) {
            return JsonProperty.USE_DEFAULT_NAME;
        }
        long startTime = SystemClock.uptimeMillis();
        try {
            XmlSerializer serializer = Xml.newSerializer();
            StringWriter stringWriter = new StringWriter();
            serializer.setOutput(stringWriter);
            serializer.startDocument("UTF-8", true);
            serializer.startTag(JsonProperty.USE_DEFAULT_NAME, "hierarchy");
            serializer.attribute(JsonProperty.USE_DEFAULT_NAME, "rotation", Integer.toString(rotation));
            dumpNodeRec(root, serializer, 0, width, height);
            serializer.endTag(JsonProperty.USE_DEFAULT_NAME, "hierarchy");
            serializer.endDocument();
            return stringWriter.toString();
        } catch (IOException e) {
            String str = LOGTAG;
            Log.e(str, "failed to dump window to file", e);
            long endTime = SystemClock.uptimeMillis();
            Log.w(str, "Fetch time: " + (endTime - startTime) + "ms");
            return JsonProperty.USE_DEFAULT_NAME;
        }
    }

    public static String dumpWindows(SparseArray<List<AccessibilityWindowInfo>> allWindows) {
        Object displayManager;
        int d;
        int nd;
        SparseArray<List<AccessibilityWindowInfo>> sparseArray = allWindows;
        if (allWindows.size() == 0) {
            return JsonProperty.USE_DEFAULT_NAME;
        }
        char c = 0;
        Object displayManager2 = DisplayManagerGlobal.getInstance.call(new Object[0]);
        long startTime = SystemClock.uptimeMillis();
        try {
            XmlSerializer serializer = Xml.newSerializer();
            StringWriter stringWriter = new StringWriter();
            serializer.setOutput(stringWriter);
            int i = 1;
            serializer.startDocument("UTF-8", true);
            serializer.startTag(JsonProperty.USE_DEFAULT_NAME, "displays");
            int nd2 = allWindows.size();
            int d2 = 0;
            while (d2 < nd2) {
                int displayId = sparseArray.keyAt(d2);
                RefMethod<Display> refMethod = DisplayManagerGlobal.getRealDisplay;
                Object[] objArr = new Object[i];
                objArr[c] = Integer.valueOf(displayId);
                Display display = refMethod.call(displayManager2, objArr);
                if (display == null) {
                    displayManager = displayManager2;
                    d = d2;
                    nd = nd2;
                } else {
                    List<AccessibilityWindowInfo> windows = sparseArray.valueAt(d2);
                    if (windows.isEmpty()) {
                        displayManager = displayManager2;
                        d = d2;
                        nd = nd2;
                    } else {
                        serializer.startTag(JsonProperty.USE_DEFAULT_NAME, "display");
                        serializer.attribute(JsonProperty.USE_DEFAULT_NAME, JsonRpcBasicServer.ID, Integer.toString(displayId));
                        int rotation = display.getRotation();
                        Point size = new Point();
                        display.getRealSize(size);
                        int n = windows.size();
                        int i2 = 0;
                        while (true) {
                            displayManager = displayManager2;
                            int n2 = n;
                            if (i2 >= n2) {
                                break;
                            }
                            try {
                                AccessibilityWindowInfo accessibilityWindowInfo = windows.get(i2);
                                int i3 = size.x;
                                int n3 = size.y;
                                int d3 = d2;
                                int d4 = i2;
                                dumpWindowRec(accessibilityWindowInfo, serializer, d4, i3, n3, rotation);
                                i2++;
                                displayManager2 = displayManager;
                                display = display;
                                n = n2;
                                windows = windows;
                                d2 = d3;
                                nd2 = nd2;
                            } catch (IOException e) {
                                e = e;
                                String str = LOGTAG;
                                Log.e(str, "failed to dump window to file", e);
                                long endTime = SystemClock.uptimeMillis();
                                Log.w(str, "Fetch time: " + (endTime - startTime) + "ms");
                                return JsonProperty.USE_DEFAULT_NAME;
                            }
                        }
                        d = d2;
                        nd = nd2;
                        serializer.endTag(JsonProperty.USE_DEFAULT_NAME, "display");
                    }
                }
                d2 = d + 1;
                sparseArray = allWindows;
                displayManager2 = displayManager;
                nd2 = nd;
                c = 0;
                i = 1;
            }
            serializer.endTag(JsonProperty.USE_DEFAULT_NAME, "displays");
            serializer.endDocument();
            return stringWriter.toString();
        } catch (IOException e2) {
            e = e2;
        }
        return "";
    }

    public static String dumpWindows(List<AccessibilityWindowInfo> windows, int displayId) throws DumpWindowException, IOException {
        Display display;
        if (windows.isEmpty()) {
            throw new DumpWindowException("windows empty");
        }
        Object displayManager = DisplayManagerGlobal.getInstance.call(new Object[0]);
        Display display2 = DisplayManagerGlobal.getRealDisplay.call(displayManager, Integer.valueOf(displayId));
        long startTime = SystemClock.uptimeMillis();
        try {
            XmlSerializer serializer = Xml.newSerializer();
            StringWriter stringWriter = new StringWriter();
            serializer.setOutput(stringWriter);
            serializer.startDocument("UTF-8", true);
            serializer.startTag(JsonProperty.USE_DEFAULT_NAME, "displays");
            serializer.startTag(JsonProperty.USE_DEFAULT_NAME, "display");
            serializer.attribute(JsonProperty.USE_DEFAULT_NAME, JsonRpcBasicServer.ID, Integer.toString(displayId));
            int rotation = display2.getRotation();
            Point size = new Point();
            display2.getRealSize(size);
            int i = 0;
            for (int n = windows.size(); i < n; n = n) {
                Object displayManager2 = displayManager;
                try {
                    display = display2;
                } catch (Throwable th) {
                    e = th;
                    long endTime = SystemClock.uptimeMillis();
                    Log.w(LOGTAG, "Fetch time: " + (endTime - startTime) + "ms");
                    throw e;
                }
                try {
                    try {
                        dumpWindowRec(windows.get(i), serializer, i, size.x, size.y, rotation);
                        i++;
                        stringWriter = stringWriter;
                        displayManager = displayManager2;
                        display2 = display;
                    } catch (Throwable th2) {
                        e = th2;
                        long endTime2 = SystemClock.uptimeMillis();
                        Log.w(LOGTAG, "Fetch time: " + (endTime2 - startTime) + "ms");
                        throw e;
                    }
                } catch (IOException e2) {
                    e = e2;
                    Log.e(LOGTAG, "failed to dump window to file", e);
                    throw e;
                }
            }
            serializer.endTag(JsonProperty.USE_DEFAULT_NAME, "display");
            serializer.endTag(JsonProperty.USE_DEFAULT_NAME, "displays");
            serializer.endDocument();
            String stringWriter2 = stringWriter.toString();
            long endTime3 = SystemClock.uptimeMillis();
            Log.w(LOGTAG, "Fetch time: " + (endTime3 - startTime) + "ms");
            return stringWriter2;
        } catch (IOException e3) {
            e = e3;
        } catch (Throwable th3) {
            e = th3;
        }
        return "";
    }

    private static void dumpWindowRec(AccessibilityWindowInfo winfo, XmlSerializer serializer, int index, int width, int height, int rotation) throws IOException {
        serializer.startTag(JsonProperty.USE_DEFAULT_NAME, "window");
        serializer.attribute(JsonProperty.USE_DEFAULT_NAME, "index", Integer.toString(index));
        CharSequence title = winfo.getTitle();
        serializer.attribute(JsonProperty.USE_DEFAULT_NAME, "title", title != null ? title.toString() : JsonProperty.USE_DEFAULT_NAME);
        Rect tmpBounds = new Rect();
        winfo.getBoundsInScreen(tmpBounds);
        serializer.attribute(JsonProperty.USE_DEFAULT_NAME, "bounds", tmpBounds.toShortString());
        serializer.attribute(JsonProperty.USE_DEFAULT_NAME, "active", Boolean.toString(winfo.isActive()));
        serializer.attribute(JsonProperty.USE_DEFAULT_NAME, "focused", Boolean.toString(winfo.isFocused()));
        serializer.attribute(JsonProperty.USE_DEFAULT_NAME, "accessibility-focused", Boolean.toString(winfo.isAccessibilityFocused()));
        serializer.attribute(JsonProperty.USE_DEFAULT_NAME, JsonRpcBasicServer.ID, Integer.toString(winfo.getId()));
        serializer.attribute(JsonProperty.USE_DEFAULT_NAME, "layer", Integer.toString(winfo.getLayer()));
        serializer.attribute(JsonProperty.USE_DEFAULT_NAME, "type", mirror.android.view.accessibility.AccessibilityWindowInfo.typeToString.call(Integer.valueOf(winfo.getType())));
        int count = winfo.getChildCount();
        for (int i = 0; i < count; i++) {
            AccessibilityWindowInfo child = winfo.getChild(i);
            if (child == null) {
                Log.i(LOGTAG, String.format("Null window child %d/%d, parent: %s", Integer.valueOf(i), Integer.valueOf(count), winfo.getTitle()));
            } else {
                dumpWindowRec(child, serializer, i, width, height, rotation);
                child.recycle();
            }
        }
        AccessibilityNodeInfo root = winfo.getRoot();
        if (root != null) {
            serializer.startTag(JsonProperty.USE_DEFAULT_NAME, "hierarchy");
            serializer.attribute(JsonProperty.USE_DEFAULT_NAME, "rotation", Integer.toString(rotation));
            dumpNodeRec(root, serializer, 0, width, height);
            root.recycle();
            serializer.endTag(JsonProperty.USE_DEFAULT_NAME, "hierarchy");
        }
        serializer.endTag(JsonProperty.USE_DEFAULT_NAME, "window");
    }

    private static void dumpNodeRec(AccessibilityNodeInfo node, XmlSerializer serializer, int index, int width, int height) throws IOException {
        serializer.startTag(JsonProperty.USE_DEFAULT_NAME, "node");
        if (!nafExcludedClass(node) && !nafCheck(node)) {
            serializer.attribute(JsonProperty.USE_DEFAULT_NAME, "NAF", Boolean.toString(true));
        }
        serializer.attribute(JsonProperty.USE_DEFAULT_NAME, "index", Integer.toString(index));
        serializer.attribute(JsonProperty.USE_DEFAULT_NAME, "text", safeCharSeqToString(node.getText()));
        serializer.attribute(JsonProperty.USE_DEFAULT_NAME, "resource-id", safeCharSeqToString(node.getViewIdResourceName()));
        serializer.attribute(JsonProperty.USE_DEFAULT_NAME, InstrumentationResultPrinter.REPORT_KEY_NAME_CLASS, safeCharSeqToString(node.getClassName()));
        serializer.attribute(JsonProperty.USE_DEFAULT_NAME, "package", safeCharSeqToString(node.getPackageName()));
        serializer.attribute(JsonProperty.USE_DEFAULT_NAME, "content-desc", safeCharSeqToString(node.getContentDescription()));
        serializer.attribute(JsonProperty.USE_DEFAULT_NAME, "checkable", Boolean.toString(node.isCheckable()));
        serializer.attribute(JsonProperty.USE_DEFAULT_NAME, "checked", Boolean.toString(node.isChecked()));
        serializer.attribute(JsonProperty.USE_DEFAULT_NAME, "clickable", Boolean.toString(node.isClickable()));
        serializer.attribute(JsonProperty.USE_DEFAULT_NAME, "enabled", Boolean.toString(node.isEnabled()));
        serializer.attribute(JsonProperty.USE_DEFAULT_NAME, "focusable", Boolean.toString(node.isFocusable()));
        serializer.attribute(JsonProperty.USE_DEFAULT_NAME, "focused", Boolean.toString(node.isFocused()));
        serializer.attribute(JsonProperty.USE_DEFAULT_NAME, "scrollable", Boolean.toString(node.isScrollable()));
        serializer.attribute(JsonProperty.USE_DEFAULT_NAME, "long-clickable", Boolean.toString(node.isLongClickable()));
        serializer.attribute(JsonProperty.USE_DEFAULT_NAME, "password", Boolean.toString(node.isPassword()));
        serializer.attribute(JsonProperty.USE_DEFAULT_NAME, "selected", Boolean.toString(node.isSelected()));
        serializer.attribute(JsonProperty.USE_DEFAULT_NAME, "bounds", AccessibilityNodeInfoHelper.getVisibleBoundsInScreen(node, width, height).toShortString());
        int count = node.getChildCount();
        for (int i = 0; i < count; i++) {
            AccessibilityNodeInfo child = node.getChild(i);
            if (child != null) {
                if (child.isVisibleToUser()) {
                    dumpNodeRec(child, serializer, i, width, height);
                    child.recycle();
                } else {
                    Log.i(LOGTAG, String.format("Skipping invisible child: %s", child.toString()));
                }
            } else {
                Log.i(LOGTAG, String.format("Null child %d/%d, parent: %s", Integer.valueOf(i), Integer.valueOf(count), node.toString()));
            }
        }
        serializer.endTag(JsonProperty.USE_DEFAULT_NAME, "node");
    }

    private static boolean nafExcludedClass(AccessibilityNodeInfo node) {
        String className = safeCharSeqToString(node.getClassName());
        for (String excludedClassName : NAF_EXCLUDED_CLASSES) {
            if (className.endsWith(excludedClassName)) {
                return true;
            }
        }
        return false;
    }

    private static boolean nafCheck(AccessibilityNodeInfo node) {
        boolean isNaf = node.isClickable() && node.isEnabled() && safeCharSeqToString(node.getContentDescription()).isEmpty() && safeCharSeqToString(node.getText()).isEmpty();
        if (isNaf) {
            return childNafCheck(node);
        }
        return true;
    }

    private static boolean childNafCheck(AccessibilityNodeInfo node) {
        int childCount = node.getChildCount();
        for (int x = 0; x < childCount; x++) {
            AccessibilityNodeInfo childNode = node.getChild(x);
            if (!safeCharSeqToString(childNode.getContentDescription()).isEmpty() || !safeCharSeqToString(childNode.getText()).isEmpty() || childNafCheck(childNode)) {
                return true;
            }
        }
        return false;
    }

    private static String safeCharSeqToString(CharSequence cs) {
        if (cs == null) {
            return JsonProperty.USE_DEFAULT_NAME;
        }
        return stripInvalidXMLChars(cs);
    }

    private static String stripInvalidXMLChars(CharSequence cs) {
        StringBuffer ret = new StringBuffer();
        for (int i = 0; i < cs.length(); i++) {
            char ch = cs.charAt(i);
            if ((ch >= 1 && ch <= '\b') || ((ch >= 11 && ch <= '\f') || ((ch >= 14 && ch <= 31) || ((ch >= 127 && ch <= 132) || ((ch >= 134 && ch <= 159) || ((ch >= 64976 && ch <= 64991) || ((ch >= 65534 && ch <= 65535) || ((ch >= 65534 && ch <= 65535) || ((ch >= 65534 && ch <= 65535) || ((ch >= 65534 && ch <= 65535) || ((ch >= 65534 && ch <= 65535) || ((ch >= 65534 && ch <= 65535) || ((ch >= 65534 && ch <= 65535) || ((ch >= 65534 && ch <= 65535) || ((ch >= 65534 && ch <= 65535) || ((ch >= 65534 && ch <= 65535) || ((ch >= 65534 && ch <= 65535) || ((ch >= 65534 && ch <= 65535) || ((ch >= 65534 && ch <= 65535) || ((ch >= 65534 && ch <= 65535) || ((ch >= 65534 && ch <= 65535) || (ch >= 65534 && ch <= 65535)))))))))))))))))))))) {
                ret.append(".");
            } else {
                ret.append(ch);
            }
        }
        return ret.toString();
    }

    /* loaded from: classes2.dex */
    public static final class DumpWindowException extends Exception {
        public DumpWindowException(String message) {
            super(message);
        }
    }
}